package ExceptionDemo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2025-03-14
 * Time: 09:13
 */
public class Test4 {

    //求数组最大值
    //throws NullPointerException表示这个getMax方法会出现这个NullPointerException，让方法调用者记得去处理
    public static int getMax(int[] arr) throws NullPointerException{
        if(arr == null){
            throw new NullPointerException("空指针异常");
        }
        int  max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(arr[i] > max){
                max = arr[i];
            }
        }
        return max;
    }
    public static void main(String[] args) throws NullPointerException{
        int[] arr = null;
        int max = 0;
        max = getMax(arr);
        /*try{
            max = getMax(arr);
        }catch (NullPointerException e){

            System.out.println("空指针异常");
        }*/
        System.out.println(max);
    }
}
